package com.bq.gametrade.ui.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.AdapterView;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bq.gametrade.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Created by Kim-wu on 2015/4/3.
 * 年、月、日弹出框
 */
public class CustomPickDateDialog implements DatePicker.OnDateChangedListener,
        AdapterView.OnItemClickListener {

    private DatePicker datePicker;
    private AlertDialog alertDialog;
    private String initDate;
    private String datetime;
    private Activity activity;

    public CustomPickDateDialog(Activity activity, String initdateTime){
        this.activity = activity;
        this.initDate = initdateTime;
    }

    public void init(DatePicker datePicker){
        Calendar calendar = Calendar.getInstance();
        if(!(null==initDate ||"".equals(initDate))){
            calendar = this.getCalendar(initDate);
        } else {
            initDate = calendar.get(Calendar.YEAR) + "-"
                    + calendar.get(Calendar.MONTH) + "-"
                    + calendar.get(Calendar.DAY_OF_MONTH) ;
        }

        datePicker.init(calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), this);

    }

    public AlertDialog dateTimePicKDialog(final TextView inputDate) {
        LinearLayout dateTimeLayout = (LinearLayout) activity
                .getLayoutInflater().inflate(R.layout.dialog_select_date, null);
        datePicker = (DatePicker) dateTimeLayout.findViewById(R.id.datepicker);
        init(datePicker);

        alertDialog = new AlertDialog.Builder(activity)
                .setTitle(initDate)
                .setView(dateTimeLayout)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        inputDate.setText(datetime);
                        inputDate.setTextColor(activity.getResources().getColor(R.color.black));
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
//                        inputDate.setText("");
                    }
                }).show();

        onDateChanged(null, 0, 0, 0);
        return alertDialog;
    }



    @Override
    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

        Calendar calendar = Calendar.getInstance();

        calendar.set(datePicker.getYear(), datePicker.getMonth(),
                datePicker.getDayOfMonth());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        datetime = sdf.format(calendar.getTime());
        alertDialog.setTitle(datetime);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        onDateChanged(null, 0, 0, 0);
    }


    private Calendar getCalendar(String initDate){
        Calendar calendar = Calendar.getInstance();
        String date = spliteString(initDate, "日", "index", "front"); // 日期
        String time = spliteString(initDate, "日", "index", "back"); // 时间

        String yearStr = spliteString(date, "年", "index", "front"); // 年份
        String monthAndDay = spliteString(date, "年", "index", "back"); // 月日

        String monthStr = spliteString(monthAndDay, "月", "index", "front"); // 月
        String dayStr = spliteString(monthAndDay, "月", "index", "back"); // 日

        int currentYear = Integer.valueOf(yearStr.trim()).intValue();
        int currentMonth = Integer.valueOf(monthStr.trim()).intValue() - 1;
        int currentDay = Integer.valueOf(dayStr.trim()).intValue();


        calendar.set(currentYear, currentMonth, currentDay);
        return calendar;
    }

    public static String spliteString(String srcStr, String pattern,
                                      String indexOrLast, String frontOrBack) {
        String result = "";
        int loc = -1;
        if (indexOrLast.equalsIgnoreCase("index")) {
            loc = srcStr.indexOf(pattern); // 取得字符串第一次出现的位置
        } else {
            loc = srcStr.lastIndexOf(pattern); // 最后一个匹配串的位置
        }
        if (frontOrBack.equalsIgnoreCase("front")) {
            if (loc != -1)
                result = srcStr.substring(0, loc); // 截取子串
        } else {
            if (loc != -1)
                result = srcStr.substring(loc + 1, srcStr.length()); // 截取子串
        }
        return result;
    }
}
